//
//  AppDelegate.m
//  meiyinxiang
//
//  Created by apple on 2017/10/16.
//  Copyright © 2017年 hbjApple. All rights reserved.
//  qppdelegate

#import "AppDelegate.h"
#import "MainTabbarViewController.h"
#import "IQKeyboardManager.h"
#import "JSHAREService.h"
//#import "CHSocialService.h"
#import <UMSocialConfig.h>
#import <AlipaySDK/AlipaySDK.h>
#import "LaunchIntroductionView.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self makeWindows];
    [self configBaiduProfile];
    [self initNIM];
    [self setupKeyBoard];
    //第三方登录配置
    [self thirdPlatform];
    //引导页
    [LaunchIntroductionView sharedWithImages:@[@"launch0",@"launch1",@"launch2",@"launch3"] buttonImage:@"lijiBtn" buttonFrame:CGRectMake((kScreen_width-190)/2, kScreen_height - 0.248*kScreen_height, 190, 36)];
    return YES;
}

-(void)initNIM {
    //推荐在程序启动的时候初始化 NIMSDK
    NSString *appKey        = MYYNIMkey;
    NIMSDKOption *option    = [NIMSDKOption optionWithAppKey:appKey];
    option.apnsCername      = MYY_apnsCername;
    option.pkCername        = MYY_pushkit_cer_name;
    [[NIMSDK sharedSDK] registerWithOption:option];
    NSString *account = [MYY_User readUserFromFile].accid;
    NSString *token   = [MYY_User readUserFromFile].imtoken;
    [[[NIMSDK sharedSDK] loginManager] login:account
                                       token:token
                                  completion:^(NSError *error) {
                                      NSLog(@"网易云---登录---error %@",error);
                                      
                                  }];
}

#pragma mark -  键盘

- (void)setupKeyBoard
{
    [[IQKeyboardManager sharedManager] setEnable:YES];
    
    [[IQKeyboardManager sharedManager] setKeyboardDistanceFromTextField:15];
    //Enabling autoToolbar behaviour. If It is set to NO. You have to manually create UIToolbar for keyboard.
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];

    
    //Resign textField if touched outside of UITextField/UITextView.
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
    
    //Giving permission to modify TextView's frame
   // [[IQKeyboardManager sharedManager] setCanAdjustTextView:YES];
    
    
}
#pragma mark -AlipayMethods
//NOTE: 9.0以上使用API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    if ([url.host isEqualToString:@"safepay"]) {
        // 支付跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
        
        // 授权跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            // 解析 auth code
            NSString *result = resultDic[@"result"];
            NSString *authCode = nil;
            if (result.length>0) {
                NSArray *resultArr = [result componentsSeparatedByString:@"&"];
                for (NSString *subResult in resultArr) {
                    if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                        authCode = [subResult substringFromIndex:10];
                        break;
                    }
                }
            }
            NSLog(@"授权结果 authCode = %@", authCode?:@"");
        }];
    }
    [JSHAREService handleOpenUrl:url];
  //  return [CHSocialServiceCenter handleOpenURL:url delegate:nil];
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  //  [CHSocialServiceCenter applicationDidBecomeActive];
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#pragma Mark - PrivateMethod
- (void)makeWindows{
    _window = [[UIWindow alloc] init];
    MainTabbarViewController *tabbarVC = [[MainTabbarViewController  alloc] init];
    _window.backgroundColor = [UIColor whiteColor];
    _window.rootViewController = tabbarVC;
    [_window makeKeyAndVisible];

}
#pragma mark -Primethod
- (void)thirdPlatform{
    JSHARELaunchConfig *config = [[JSHARELaunchConfig alloc] init];
    config.appKey = @"b41bc10d016b99d644d0e5e2";
    config.SinaWeiboAppKey = @"3188970509";
    config.SinaWeiboAppSecret = @"2612a081077b48a9a24c7a700ec402f0";
    config.SinaRedirectUri = @"http://meiyinxiang.net";
    config.QQAppId = @"1106567040";
    config.QQAppKey = @"PiXml5If5XZ9XAKx";
    config.WeChatAppId = @"wx68f0429fdb652334";
    config.WeChatAppSecret = @"abaa8df64bbfdaf4506874cd340d58d9";
    [JSHAREService setupWithConfig:config];
    [JSHAREService setDebug:YES];

}
#pragma Mark -BaiduMethod
- (void)configBaiduProfile{
    self.mCityName = @"合肥市";
    _mapManager = [[BMKMapManager alloc] init];
    [[BMKLocationAuth sharedInstance] checkPermisionWithKey:@"yhcMubBakbWsk7WFHbTLiSGaUDuxbY5L" authDelegate:nil];
    BOOL ret = [_mapManager start:@"yhcMubBakbWsk7WFHbTLiSGaUDuxbY5L" generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager star failed");
    }
    
}


@end
